package cn.edu.niit.alienlab.jshttplessonserver.apis;

import cn.edu.niit.alienlab.jshttplessonserver.dto.MenuGroup;
import cn.edu.niit.alienlab.jshttplessonserver.dto.MenuItem;
import cn.edu.niit.alienlab.jshttplessonserver.dto.UserToken;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class RouterResource {
    @PostMapping("/admin/login")
    public ResponseEntity doLogin(
            @RequestParam(required = true) String username,
            @RequestParam(required = true)String password){
        if((username.equals("admin") && password.equals("admin123"))
                || (username.equals("user") && password.equals("user123"))
                || (username.equals("test") && password.equals("test123"))
        ){
            UserToken token=new UserToken();
            token.setId_token(username);
            return ResponseEntity.ok(token);
        }else{
            return ResponseEntity.status(401).body("用户名密码错误");
        }
    }

    @GetMapping("/admin/menus")
    public ResponseEntity getMenus(HttpServletRequest request){
        String authorization=request.getHeader("Authorization");
        if(authorization==null){
            return ResponseEntity.status(401).body("用户未登录");
        }else if(authorization.equals("admin") || authorization.equals("user")){
            List<MenuGroup> groups=new ArrayList<MenuGroup>();
            MenuGroup group1=new MenuGroup();
            group1.setGroupName("影片库");
            group1.setGroupCode("movieRepository");
            group1.setSort("1");
            group1.setItems(new ArrayList<MenuItem>());
            MenuItem menuItem1=new MenuItem();
            menuItem1.setMenuName("影片查询");
            menuItem1.setComponent("../views/admin/movies/movielist.vue");
            menuItem1.setHref("list-movie");
            menuItem1.setSort("1");
            group1.getItems().add(menuItem1);
            MenuItem menuItem2=new MenuItem();
            menuItem2.setMenuName("创建影片");
            menuItem2.setComponent("../views/admin/movies/movieadd.vue");
            menuItem2.setHref("create-movie");
            menuItem2.setSort("2");
            group1.getItems().add(menuItem2);
            MenuItem menuItem3=new MenuItem();
            menuItem3.setMenuName("影评查询");
            menuItem3.setHref("comment-search");
            menuItem3.setSort("3");
            group1.getItems().add(menuItem3);

            groups.add(group1);

            MenuGroup group2=new MenuGroup();
            group2.setGroupName("演员库");
            group2.setGroupCode("actorRepository");
            group2.setSort("2");
            group2.setItems(new ArrayList<MenuItem>());
            MenuItem menuItem21=new MenuItem();
            menuItem21.setMenuName("演员查询");
            menuItem21.setHref("search-actors");
            menuItem21.setSort("1");
            group2.getItems().add(menuItem21);

            MenuItem menuItem22=new MenuItem();
            menuItem22.setMenuName("新增演员");
            menuItem22.setHref("add-actors");
            menuItem22.setSort("2");
            group2.getItems().add(menuItem22);
            groups.add(group2);

            MenuGroup group3=new MenuGroup();
            group3.setGroupName("基础数据");
            group3.setGroupCode("dataRepository");
            group3.setSort("3");
            group3.setItems(new ArrayList<MenuItem>());

            MenuItem menuItem31=new MenuItem();
            menuItem31.setMenuName("分类管理");
            menuItem31.setHref("movie-types");
            menuItem31.setSort("1");
            group3.getItems().add(menuItem31);

            MenuItem menuItem32=new MenuItem();
            menuItem32.setMenuName("评论管理");
            menuItem32.setHref("movie-comments");
            menuItem32.setSort("2");
            group3.getItems().add(menuItem32);

            MenuItem menuItem33=new MenuItem();
            menuItem33.setMenuName("国别管理");
            menuItem33.setHref("movie-contries");
            menuItem33.setSort("3");
            group3.getItems().add(menuItem33);

            groups.add(group3);

            if(authorization.equals("admin")){
                MenuGroup group4=new MenuGroup();
                group4.setGroupName("系统设置");
                group4.setGroupCode("systemRepository");
                group4.setSort("4");
                group4.setItems(new ArrayList<MenuItem>());

                MenuItem menuItem41=new MenuItem();
                menuItem41.setMenuName("用户查询");
                menuItem41.setHref("search-users");
                menuItem41.setSort("1");
                menuItem41.setHasRole("admin");
                group4.getItems().add(menuItem41);

                MenuItem menuItem42=new MenuItem();
                menuItem42.setMenuName("账户管理");
                menuItem42.setHref("system-account");
                menuItem42.setSort("2");
                menuItem42.setHasRole("admin");
                group4.getItems().add(menuItem42);
                groups.add(group4);
            }
            return ResponseEntity.ok(groups);
        }else{
            return ResponseEntity.status(403).body("登录用户无权限");
        }
    }
}
